package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import zhihuilan.common.utils.MD5EncryptionUtils;
import zhihuilan.common.utils.ToolsUtils;
import zhihuilan.dto.MyShopDto;
import zhihuilan.dto.MyStaffShopDto;
import zhihuilan.dto.MyStaffShopGoodsDto;
import zhihuilan.dto.WxMiniPageDto;
import zhihuilan.entity.MyAcquisition;
import zhihuilan.entity.MyFactoryFav;
import zhihuilan.entity.MyShop;
import zhihuilan.entity.MyStaffShop;
import zhihuilan.mapper.MyFactoryFavMapper;
import zhihuilan.mapper.MyShopMapper;
import zhihuilan.params.query.MyShopQueryParam;
import zhihuilan.service.MyShopService;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户数据 服务实现
 *
 * @author ruoyi
 */
@Service
public class MyShopServiceImpl extends ServiceImpl<MyShopMapper, MyShop> implements MyShopService {

    @Resource
    private MyFactoryFavMapper myFactoryFavMapper;


    @Override
    public Page<MyShop> selectPageList(MyShopQueryParam queryParam) {
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        QueryWrapper<MyShop> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam.getLng()) && !ObjectUtils.isEmpty(queryParam.getLat())) {
            queryWrapper.select("*", ToolsUtils.getDistanceSqlTencent(queryParam.getLat(), queryParam.getLng()) + " * 1000 as distance ");
        } else {
            queryWrapper.select("*", " 0 as distance ");
        }
        queryWrapper.eq("status", 1);
        queryWrapper.eq("audit", 1);
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getStaffid())) {
                queryWrapper.eq("staffid", queryParam.getStaffid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getPid())) {
                queryWrapper.like("pid", queryParam.getPid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getStatus())) {
                queryWrapper.eq("status", queryParam.getStatus());
            } else {
                queryWrapper.eq("status", 1);
            }
            if (!ObjectUtils.isEmpty(queryParam.getAudit())) {
                queryWrapper.eq("audit", queryParam.getAudit());
            }
            if (!ObjectUtils.isEmpty(queryParam.getKeyword())) {
                queryWrapper.apply(" CONCAT(title , title1 , contact , telephone) like '%" + queryParam.getKeyword() + "%'");
            }
            if (!ObjectUtils.isEmpty(queryParam.getDistrictid())) {
                queryWrapper.likeRight("districtid", queryParam.getDistrictid());
            }
        }
        if (!ObjectUtils.isEmpty(queryParam.getRand())) {
            queryWrapper.orderByDesc("rand(" + queryParam.getRand() + ")");
        } else {
            queryWrapper.orderByDesc("id");
        }

        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (pageNum == 1) {
            pageNum = 0;
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        Page<MyShop> page = new Page<>(pageNum, pageSize);
        this.baseMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public List<MyShop> selectList(MyShopQueryParam queryParam) {
        QueryWrapper<MyShop> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getGuid())) {
                queryWrapper.eq("guid", queryParam.getGuid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getStaffid())) {
                queryWrapper.eq("staffid", queryParam.getStaffid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getStatus())) {
                queryWrapper.eq("status", queryParam.getStatus());
            } else {
                queryWrapper.eq("status", 1);
            }
            if (!ObjectUtils.isEmpty(queryParam.getAudit())) {
                queryWrapper.eq("audit", queryParam.getAudit());
            }
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public Long selectCount(MyShopQueryParam queryParam) {
        QueryWrapper<MyShop> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getGuid())) {
                queryWrapper.eq("guid", queryParam.getGuid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getStaffid())) {
                queryWrapper.eq("staffid", queryParam.getStaffid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getStatus())) {
                queryWrapper.eq("status", queryParam.getStatus());
            } else {
                queryWrapper.eq("status", 1);
            }
            if (!ObjectUtils.isEmpty(queryParam.getAudit())) {
                queryWrapper.eq("audit", queryParam.getAudit());
            }
        }
        return this.baseMapper.selectCount(queryWrapper);
    }

    @Override
    public MyShop selectShopByById(Integer id) {
        return this.baseMapper.selectById(id);
    }


    @Override
    public MyShop selectShopByStaffId(Integer staffId) {
        QueryWrapper<MyShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staffid", staffId);
        queryWrapper.eq("status", 1);
//        queryWrapper.eq("status",1);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<MyShop> selectShopIdByShopName(String shopName) {
        QueryWrapper<MyShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.like("title", shopName);
        queryWrapper.like("title1", shopName);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Boolean isShopCreateed(Integer staffId) {
        QueryWrapper<MyShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staffid", staffId);
        queryWrapper.eq("audit", 1);
        queryWrapper.eq("status", 1);
        return this.baseMapper.selectCount(queryWrapper) > 0 ? true : false;
    }

    @Override
    public Boolean updateShopStatus(Integer id, Integer status) {
        QueryWrapper<MyShop> queryWrapper = new QueryWrapper<>();
        MyShop entity = this.baseMapper.selectById(id);
        entity.setStatus(status);
        return this.baseMapper.updateById(entity) > 0 ? true : false;
    }

    @Transactional
    @Override
    public MyShop addEntity(MyShop entity) throws RuntimeException {
        try {
            if (ObjectUtils.isEmpty(entity)) {
                return null;
            }
            if (!ObjectUtils.isEmpty(entity.getId()) && entity.getId() == 0) {
                return null;
            }
            this.baseMapper.insert(entity);
            String guid = MD5EncryptionUtils.md5(entity.getId() + "");
            entity.setGuid(guid);
            this.baseMapper.updateById(entity);
            return entity;
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public MyShop updateEntity(MyShop entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public MyShop selectOne(Integer staffId, Integer audit, Integer status) {
        QueryWrapper<MyShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staffid", staffId);
        queryWrapper.eq("audit", audit);
        if (!ObjectUtils.isEmpty(status)) {
            queryWrapper.eq("status", status);
        } else {
            queryWrapper.eq("status", 1);
        }
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean isValidName(String title) {
        QueryWrapper<MyShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title", title);
//        queryWrapper.eq("audit", 1);
        MyShop entity = this.baseMapper.selectOne(queryWrapper);
        return !ObjectUtils.isEmpty(entity);
    }
}
